/**
  *  main.c 
  *
  */ 

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "system_stm32f10x.h"
#include "stm32f10x.h"
#include "SystemConfig.h"
#include "stm32f10x_it.h"
#include "stdlib.h"
#include "string.h"

#include "Console.h"
#include "Queue.h"
#include "spi_flash.h"
#include "memory.h"
#include "command.h"
#include "task.h"
#include "message.h"
#include "xstring.h"
#include "led.h"
#include "LSM303DLH.h"
#include "tool.h"
#include <stdio.h>
#include "gps.h"
#include "ADXL335.h"
#include "Murata.h"
#include "ELM327.h"
#include "MS5801.h"
#include "ADXL345.h"
#include "L3G4200D.h"
#include "HMC5883.h"
#include "CarState.h"
#include "imu_process.h"
#include "matrix_math.h"
#include "KamanFilter_Process.h"

/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup SysTick
  * @{
  */ 

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static volatile unsigned long TimingDelay;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

#define MX25L6406_FLASH_ID      0xC22017
#define AT25DF641_FLASH_ID      0x1F4800

APPMODE AppMode;
unsigned char L3G4200D_ID = 0;

//fenris matrix test 20110608
/*
double AAA[8] = {2,3,1,2,3,4,5,6};
double BBB[8] = {3,2,1,2,3,4,5,6};
double YYY[6] = {2,2,1,2,3,4};
double XXX[6] = {2,2,0,0,0,0};
double BTT[8] = {2,3,0,0,0,0,0,0};
*/
double KAMAN_TEST[10] = { 1.0, 0.3, 0.7, 0.2, -0.4, 1.0, -0.2, -0.6, 0.1, 0.5 };

int main(void)
{
    asm ("mov32 r0, #0x08002000 \n"
		 "ldr	r0, [r0] \n"
		 "mov	sp, r0");
	
#if (DEBUG_MODE)
	DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STOP | DBGMCU_STANDBY, ENABLE);
	
	if (CMD_DATA_SIZE > 2048)
		while(1);
	
	if (MEMORY_END > 0x20002000)
		while(1);
#else
	DBGMCU_Config(0x001FFF07, DISABLE);
#endif
	
	xmemset((void *)MEMORY_START, 0, MEMORY_END - MEMORY_START);
    gSystick = 0;
    gRTCSec = 0;
    System_Init();
    Console_Init();
    Msg_Init();
    LED_Init();
	HMC5883_Init();
    GPS_Init();
	GSM_Init();
    ADXL335_Init();
    Murata_Init();
    ELM327_Init();
	///////ADXL345_Init();
	///////ADXL345_45_Init();
	//L3G4200D_Init();
    SPI_Flash_Init(AT25DF641_FLASH_ID);
	
    SPI_Flash_Power_On();
    SPI_FLASH_DisadbleProtection();
	

	TaskInit();
	Console_Write("BX1 Start\r\n", 11);

GsmUart_Write("AT\r\n",4);
	
	/* Task Run */
//rex	TaskAdd(Imu_Param_Set,100);
//rex    TaskAdd(Param_Check_Task, 100);
    TaskAdd(Msg_Input, 0);
	TaskAdd(Msg_Output, 0);
    TaskAdd(LED_TEST_Task, 200);
//rex    TaskAdd(GPS_POTOCOL_SW_Task, 3000);
    TaskAdd(GPS_Task, 10);
//rex	TaskAdd(Azimuth_By_Mang_and_Gyro_Task, 40);
	
	
//rex    TaskAdd(MS_Pressure_Task, 10);

	TaskRun();
}

void Param_Check_Task(void)
{
  
    if (*pFlag_sys_para_rdy == 0)
	{
		TaskAdd(Load_Cmd_Data_Task, 10);
		TaskDel(Param_Check_Task);
	}
}



void Write_Backup_Reg(unsigned short id, unsigned char value)
{
	var16 val;

	val.u16 = BKP_ReadBackupRegister((id & 0x00FF));

	if ((id & 0x0100))
	{
		val.u8.lo = value;
	}
	else
	{
		val.u8.hi = value;
	}

	BKP_WriteBackupRegister((id & 0x00FF), val.u16);
}

void UpdateRTC_Hook(void)
{
//	gpss_time = RTC_GetCounter();
//	pdsr_time = RTC_GetCounter();
//	pdsr_gprs_time = RTC_GetCounter();
//	wakeup_time = RTC_GetCounter();
//	COMM_Clear_Connect_Time();
}
